import { Inject, Injectable } from '@nestjs/common';
import { AxiosService } from '@/module/axios/axios.service';
import { Result } from '@/common/utils/result';
import { QUERY_ERROR_CODE } from '@/common/constant';
import { to } from '@/common/utils/error-handler';
import { RedisService } from '@/module/redis/redis.service';

@Injectable()
export class GiteeService {


  @Inject(AxiosService)
  private readonly axiosService: AxiosService;

  @Inject(RedisService)
  private readonly redisService: RedisService;


  private readonly giteeUrl = `https://gitee.com/organizations/wei_pengcheng/event_list?url=%2Forganizations%2Fwei_pengcheng%2Fevent_list&scope=all&day=&start_date=&end_date=&year=&limit=15&project_ids=&user_ids=&prev_id=&_=1742372317078`;

  //获取gitee的动态
  async getGiteeNews() {

    if (await this.redisService.get('gitee:news')) {
      return Result.ok(await this.redisService.get('gitee:news'));
    } else {
      const result = await to(this.axiosService.getRequest(this.giteeUrl));
      if (!result.ok) return Result.fail(QUERY_ERROR_CODE, '获取失败');
      await this.redisService.set('gitee:news', result.value, 60 * 60 * 3);
      return Result.ok(result.value);
    }
  }
}
